import { ref, markRaw, onMounted, watch } from 'vue';
import { GetTaskDataSummaryListParams } from '@/apis/eci/carbonManagement/model/dataListModel';
import { getTaskDataCollectionList } from '@/apis/eci/carbonManagement/dataList';
import { SearchOptionsType } from '@/components/CPageTable';
import { useRoute } from 'vue-router';

export const useList = (props: any) => {
  const route = useRoute();
  const loading = ref(false);
  const dataSource = ref<any[]>([]);
  // 页面标题
  const taskNo = route.query.taskNo as any;
  // 搜索配置
  const searchOptions: SearchOptionsType[] = markRaw([]);
  let spanEmissionScope: any[] = [];
  let spanEmissionCategory: any[] = [];
  let spanGhgSourceType: any[] = [];

  const posSpanEmissionScope = ref(0); // 范围
  const posSpanEmissionCategory = ref(0); // 排放类别
  const posGhgSourceType = ref(0); // 温室气体类型
  const getSpanArr = (data: any) => {
    for (let i = 0; i < data.length; i++) {
      if (i === 0) {
        spanEmissionScope.push(1);
        posSpanEmissionScope.value = 0;
      } else {
        // 判断当前元素与上一个元素是否相同
        if (data[i].emissionScope === data[i - 1].emissionScope) {
          spanEmissionScope[posSpanEmissionScope.value] += 1;
          spanEmissionScope.push(0);
        } else {
          spanEmissionScope.push(1);
          posSpanEmissionScope.value = i;
        }
      }

      if (i === 0) {
        spanEmissionCategory.push(1);
        posSpanEmissionCategory.value = 0;
      } else {
        // 判断当前元素与上一个元素是否相同
        if (
          data[i].emissionCategory === data[i - 1].emissionCategory &&
          data[i].emissionScope === data[i - 1].emissionScope
        ) {
          spanEmissionCategory[posSpanEmissionCategory.value] += 1;
          spanEmissionCategory.push(0);
        } else {
          spanEmissionCategory.push(1);
          posSpanEmissionCategory.value = i;
        }
      }

      if (i === 0) {
        spanGhgSourceType.push(1);
        posSpanEmissionCategory.value = 0;
      } else {
        // 判断当前元素与上一个元素是否相同
        if (
          data[i].ghgSourceName === data[i - 1].ghgSourceName &&
          data[i].emissionCategory === data[i - 1].emissionCategory
        ) {
          spanGhgSourceType[posGhgSourceType.value] += 1;
          spanGhgSourceType.push(0);
        } else {
          spanGhgSourceType.push(1);
          posGhgSourceType.value = i;
        }
      }
    }
  };
  const cellMerge = ({ rowIndex, columnIndex }: any) => {
    if (columnIndex === 0) {
      const _row = spanEmissionScope[rowIndex];
      const _col = _row > 0 ? 1 : 0;
      return {
        rowspan: _row,
        colspan: _col
      };
    }
    if (columnIndex === 1) {
      const _row = spanEmissionCategory[rowIndex];
      const _col = _row > 0 ? 1 : 0;
      return {
        rowspan: _row,
        colspan: _col
      };
    }
    if (columnIndex === 2) {
      const _row = spanGhgSourceType[rowIndex];
      const _col = _row > 0 ? 1 : 0;
      return {
        rowspan: _row,
        colspan: _col
      };
    }
  };

  onMounted(() => {
    loadData();
  });
  function loadData() {
    const params: GetTaskDataSummaryListParams = {
      taskNo,
      standard: props.standard
    };
    loading.value = true;
    getTaskDataCollectionList(params)
      .then((res: any) => {
        if (res) {
          spanEmissionScope = [];
          spanEmissionCategory = [];
          spanGhgSourceType = [];
          posSpanEmissionScope.value = 0;
          posSpanEmissionCategory.value = 0;
          posGhgSourceType.value = 0;
          getSpanArr(res);
          dataSource.value = res;
        } else {
          dataSource.value = [];
        }
        loading.value = false;
      })
      .catch(error => {
        console.log('error', error);
        loading.value = false;
        dataSource.value = [];
      });
  }
  const extraTableOption = {
    spanMethod: cellMerge,
    maxHeight: '540'
  };
  return {
    // 头部右侧筛选项配置
    searchOptions,
    loadData,
    dataSource,
    loading,
    extraTableOption
  };
};
